# -*- coding: utf-8 -*-
"""
@Time ： 2023/5/8 0008 15:50
@Auth ： ysc
@File ：LoginPage.py
@IDE ：PyCharm
"""
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from Lib.common_lib import LOG_INFO, LOG_ERROR


class LoginPage(object):
    """首页和登录页面元素"""
    usernameBy = None
    passwordBy = None
    submitBy = None

    def __init__(self, driver):
        """
        加载驱动、日志类
        """
        self.driver = driver

    def get_url(self, url):
        """
        进入网址页面
        """
        self.driver.get(url)

    def user_button(self):
        """
        用户登录按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/header/div[2]/button/span').click()
        except Exception as e:
            message = '下拉框元素定位失败'
            LOG_ERROR("error: found user_button timeout")
            print(e)
            LOG_INFO(message)

    def user_login(self):
        """
        用户登录
        """
        try:
            self.user_button()
            time.sleep(2)
            # 用户名输入框
            self.usernameBy = self.driver.find_element(By.XPATH, '/html/body/div/div[1]/div/div[2]/form/div[1]/div/div['
                                                                 '1]/div/input')

            # 密码输入框
            self.passwordBy = self.driver.find_element(By.XPATH, '/html/body/div/div[1]/div/div[2]/form/div['
                                                                 '2]/div/div[1]/div/input')
            # 协议选项
            self.permission = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/form/div['
                                                                 '3]/div/div/div/label')
            # 登录按钮
            self.submitBy = self.driver.find_element(By.XPATH,
                                                     '//*[@id="app"]/div[1]/div/div[2]/form/div[4]/div/button')
        except Exception as e:
            message = '登录元素定位失败'
            LOG_ERROR("error: found user_login timeout")
            print(e)
            LOG_INFO(message)

    def login_valid(self, username, password):
        """
        登录验证、输入相关用户名、密码
        """
        self.usernameBy.send_keys(username)
        self.passwordBy.send_keys(password)
        self.permission.click()
        self.submitBy.click()


    def user_selector(self):
        """
        用户登录成功后点击用户下拉框
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/header/div[2]/div[1]/span/img').click()
        except Exception as e:
            message = '下拉框元素定位失败'
            LOG_ERROR("error: found user_selector timeout")
            print(e)
            LOG_INFO(message)

    def user_logout(self):
        """
        用户退出按钮
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div/div[1]/div/ul/li[2]').click()
        except Exception as e:
            message = '退出按钮元素定位失败'
            LOG_ERROR("error: found user_logout timeout")
            print(e)
            LOG_INFO(message)

    def user_page(self):
        """
        用户空间按钮
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[2]//div[1]/div/div[1]/div/ul/li[1]').click()
        except Exception as e:
            message = '用户空间按钮元素定位失败'
            LOG_ERROR("error: found user_page timeout")
            print(e)
            LOG_INFO(message)

    def image_build(self):
        """
        镜像定制查看按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/'
                                               'div/section/main/div[2]/div[1]/button/span').click()
        except Exception as e:
            message = '镜像定制查看按钮元素定位失败'
            LOG_ERROR("error: found image_build timeout")
            print(e)
            LOG_INFO(message)

    def projects_build(self):
        """
        构建工程查看按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/'
                                               'div/section/main/div[1]/div[1]/button').click()
        except Exception as e:
            message = '构建工程查看按钮元素定位失败'
            LOG_ERROR("error: found projects_build timeout")
            print(e)
            LOG_INFO(message)

    def switch_language(self):
        """
        语言栏选择界面
        """
        try:
            mouse = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/header/div[2]/div/div/div')
            ActionChains(self.driver).move_to_element(mouse).perform()
        except Exception as e:
            message = '语言栏选择界面元素定位失败'
            LOG_ERROR("error: found switch_language timeout")
            print(e)
            LOG_INFO(message)

    def switch_English(self):
        """
        切换英文
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[last()]/div/div/div[1]/div/ul/li[2]').click()
        except Exception as e:
            message = '英文按钮元素定位失败'
            LOG_ERROR("error: found switch_English timeout")
            print(e)
            LOG_INFO(message)

    def switch_Chinese(self):
        """
        切换中文
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[last()]/div/div/div[1]/div/ul/li[1]').click()
        except Exception as e:
            message = '中文按钮元素定位失败'
            LOG_ERROR("error: found switch_English timeout")
            print(e)
            LOG_INFO(message)

    def alert_message(self):
        """
        获取弹窗元素
        """
        mouse_alert = self.driver.find_element(By.XPATH, '/html/body/div[3]')
        ActionChains(self.driver).move_to_element(mouse_alert).perform()
        alert = self.driver.find_element(By.XPATH, '/html/body/div[3]')

        return alert.text
